<?php
class JobController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('view','create','update','admin','delete'
					, 'statisticsTime', 'statisticsTimeSubmit' ,'statisticsUser','statisticsUserSubmit' 
					,'statisticsUserView', 'statisticsUserExcel','statisticsTimeExcel' , 
					'statisticsJobGroup' , 'statisticsJobGroupSubmit' , 'statisticsJobGroupExcel' ,
					'report' , 'reportSubmit' , 'reportExcel' 
					),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		if(!Yii::app()->user->checkAccess('clerk'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		if(!Yii::app()->user->checkAccess('clerk'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job;
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		if(isset($_POST['Job']))
		{			
			$model->attributes=$_POST['Job'];
			$model->is_deleted = 0;
			$model->user_id = Yii::app()->user->id;
			if($model->validate()){					
                                $model->received_time = Common::convertDateTimeToString($model->received_time);
				$model->finished_time = Common::convertDateTimeToString($model->finished_time);
				if($model->save(false))
					$this->redirect(array('view','id'=>$model->id));
			}
		}
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		if(!Yii::app()->user->checkAccess('clerk',$model->user_id))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		if(isset($_POST['Job']))
		{			
			$model->attributes=$_POST['Job'];
			if($model->validate()){
				$model->received_time = Common::convertDateTimeToString($model->received_time);
				$model->finished_time = Common::convertDateTimeToString($model->finished_time);
				if($model->save(false))
					$this->redirect(array('view','id'=>$model->id));
			}

		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id);
		if(!Yii::app()->user->checkAccess('clerk',$model->user_id))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->deleteEx();
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}


	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		if(!Yii::app()->user->checkAccess('clerk'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values
		$model->user_id = Yii::app()->user->id;
		$model->received_time = date("Y-m-d");
		
		if(isset($_GET['Job']))
		{
			if(isset($_GET['Job']['received_time']))
			{
				if($_GET['Job']['received_time'] != NULL)
				{
					$time = Yii::app()->dateFormatter->formatDateTime(time(), false, 'medium'); 
					$_GET['Job']['received_time'] .= ' ' . $time;
					$_GET['Job']['received_time'] = date('Y-m-d', strtotime($_GET['Job']['received_time']));	
				}
			}
			if(isset($_GET['Job']['finished_time']))
			{
				if($_GET['Job']['finished_time'] != NULL)
				{
					$time = Yii::app()->dateFormatter->formatDateTime(time(), false, 'medium'); 
					$_GET['Job']['finished_time'] .= ' ' . $time;
					$_GET['Job']['finished_time'] = date('Y-m-d', strtotime($_GET['Job']['finished_time']));	
				}
			}
			$model->attributes=$_GET['Job'];	
		}
		
		$this->render('admin',array(
			'model'=>$model,
			)
		);
	}
	
	/**
	 * report job
	 */
	
	public function actionReport()
	{
		if(!Yii::app()->user->checkAccess('clerk'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values	
		$submit = false;
		if(isset($_POST['Job']))
		{			
			$model->attributes=$_POST['Job'];
			$submit = true;
		}
		$this->render('report',array(
			'model'=>$model,
			'submit' => $submit ,
		));
	}
	
	// action submit report
	public function actionReportSubmit()
	{
		if(!Yii::app()->user->checkAccess('clerk'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values	
		$submit = true;
		if(isset($_GET['Job']))
		{			
			$model->attributes = $_GET['Job'];
			$model->received_time = date("Y-m-d" , strtotime($model->received_time));
			$model->is_deleted = 0;
			$model->user_id = Yii::app()->user->id;
			$submit = true;
		}
		$this->render('report',array(
			'model'=>$model,
			'submit' => $submit ,
		));
	
	}
	/**
	 * Export statistics by time
	 */

	public function actionReportExcel()
	{
		if(!Yii::app()->user->checkAccess('clerk'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Job']))
		{			
			$received_time = $_GET['Job']['received_time'];			
			$listJob = $model->getListJobByReceivedTime($received_time);
			$model->attributes = $_GET['Job'];			
			$title = 'CHI TIẾT CA TRỰC HELPDESK+ ONSITE NGÀY ' . mb_strtoupper($received_time , 'UTF-8');
			$type = 'user';
		}
		$this->exportJobExcel($listJob ,$type , $title);
		$submit = true;
		$this->render('report',array(
			'model'=>$model,
			//'error' => $error ,
			'submit' => $submit ,
		));	
	}
	/**
	 * Statistics job by time 	 
	 */
	
	public function actionStatisticsTime()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job;//('searchByTime');
		$model->unsetAttributes();  // clear any default values
		$submit = false;		
                if(!isset($_GET['Job']))
                {
                   $submit = false;
                }
                $time = array();
		if(isset($_GET['Job']))
		{				
			//$model->attributes=$_POST['Job'];
			$time = $_GET['Job'];
			$date1 = $time['date1'];
			$date2 = $time['date2'];
			$model->date1 = $date1;
			$model->date2 = $date2;	
			$submit = true;
		}
		
		$this->render('statisticsTime',array(
			'model'=>$model,
			'time' => $time ,
			'submit'=>$submit 
		));
	}
	public function actionStatisticsTimeSubmit()
	{
		$model=new Job;//('searchByTime');
		$model->unsetAttributes();  // clear any default values
		$submit = true;
		$time = array();
		if(isset($_GET['Job']))
		{				
			//$model->attributes=$_POST['Job'];
			$time = $_GET['Job'];
			$date1 = $time['date1'];
			$date2 = $time['date2'];
			$model->date1 = $date1;
			$model->date2 = $date2;	
			$submit = true;
		}
		
		$this->render('statisticsTime',array(
			'model'=>$model,
			'time' => $time ,
			'submit'=>$submit 
		));
	}
	/**
	 * Export statistics by time
	 */
	
	public function actionStatisticsTimeExcel()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('searchByTime');
		$model->unsetAttributes();  // clear any default values
		$time = array();
		if(isset($_GET['Job']))
		{			
			$time = $_GET['Job'];
			$date1 = $time['date1'];
			$date2 = $time['date2'];
			$model->date1 = $date1;
			$model->date2 = $date2;		
			$listJob = $model->getListJobByTime($date1 , $date2);		
			$title = 'CHI TIẾT CA TRỰC HELPDESK+ ONSITE TỪ ' . mb_strtoupper($date1 , 'UTF-8') . ' TỚI ' . mb_strtoupper($date2 , 'UTF-8');
			$type = 'time';			
		}
		$this->exportJobExcel($listJob ,$type , $title);
		//$error = 'Dữ liệu trống';
		$submit = true;
		$this->render('statisticsTime',array(
			'model'=>$model,
			'time' => $time ,
			//'error' => $error ,
			'submit' => $submit
		));
	}
	/**
	 * Statistics job by user 	 
	 */
	
	public function actionStatisticsUser()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('searchByUser');
		$model->unsetAttributes();  // clear any default values	
		$submit = false;
		if(isset($_POST['Job']))
		{			
			$model->attributes=$_POST['Job'];
			$submit = true;
		}
		$this->render('statisticsUser',array(
			'model'=>$model,
			'submit' => $submit ,
		));
	}
	public function actionStatisticsUserSubmit()
	{
		$model=new Job('searchByUser');
		$model->unsetAttributes();  // clear any default values	
		$submit = true;
		if(isset($_GET['Job']))
		{			
			$model->attributes=$_GET['Job'];
			$submit = true;
		}
		$this->render('statisticsUser',array(
			'model'=>$model,
			'submit' => $submit ,
		));
	}
	public function actionStatisticsUserView()
	{
		//die();
		$model=new Job('searchByUser');
		$model->unsetAttributes();  // clear any default values		
		if(isset($_GET['Job']))
		{			
			$model->attributes=$_GET['Job'];			
			//die(print_r($model->attributes));	
		}
		$this->render('statisticsUserView',array(
			'model'=>$model,
		));
	}	
	
	public function actionStatisticsUserExcel()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('searchByUser');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Job']))
		{
			$model->attributes=$_GET['Job'];
			$user_id = $_GET['Job']['user_id'];
			$listJob = $model->getListJobByUserId($user_id);	
			$arrayUser = $model->getUser($user_id);	
			$fullName = $arrayUser[0]->attributes['full_name'];
			$title = 'CHI TIẾT CA TRỰC HELPDESK+ ONSITE CỦA NHÂN VIÊN ' . mb_strtoupper($fullName, 'UTF-8');
			$type = 'user';			
		}
		$this->exportJobExcel($listJob , $type , $title);
		//$error = 'Dữ liệu trống';
		$submit = true;
		$this->render('statisticsUser',array(
			'model'=>$model,
			'error' => $error ,
			'submit' => $submit 
		));
	}
	
	/**
	 * Statistics by jobgroup
	 */
	public function actionStatisticsJobGroup()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values	
		$submit = false;
		if(isset($_GET['Job']))
		{			
			$model->attributes=$_GET['Job'];
			$submit = true;
		}
		$this->render('statisticsJobGroup',array(
			'model'=>$model,
			'submit'=>$submit
		));
	}
	/**
	 * Statistics by jobgroup when submit form
	 */
	public function actionStatisticsJobGroupSubmit()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values	
		$submit = true;
		if(isset($_GET['Job']))
		{			
			//die(print_r($_POST['Job']));
			$model->attributes=$_GET['Job'];
			$submit = true;
		}
		$this->render('statisticsJobGroup',array(
			'model'=>$model,
			'submit'=>$submit
		));
	}
	/**
	 * action export excel statistics by jobgroup
	 */
	public function actionStatisticsJobGroupExcel()
	{
		if(!Yii::app()->user->checkAccess('manager'))
		{
			throw new CHttpException(403,'Bạn không có quyền hạn truy cập trang này.');
		}
		$model=new Job('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Job']))
		{
			$model->attributes=$_GET['Job'];
			$job_group_id = $_GET['Job']['job_group_id'];
			$listJob = $model->getListJobByGroupId($job_group_id);
			$arrayJobGrop = $model->getJobGroup($job_group_id);			
			$groupName = $arrayJobGrop[0]->attributes['name'];
			$title = 'CHI TIẾT CA TRỰC HELPDESK+ ONSITE CỦA NHÓM CÔNG VIỆC ' . mb_strtoupper($groupName, 'UTF-8');
			$type = 'job_group';	
		}
		$this->exportJobExcel($listJob , $type ,$title);
		$sumit = true;
		$this->render('statisticsJobGroup',array(
			'model'=>$model,
			'submit' => $sumit ,
			
		));
	}
	
	public function exportJobExcel($data , $type , $title)
	{
                $objPHPExcel = Yii::app()->excel;
		$objPHPExcel->setActiveSheetIndex(0);
		
		$sharedStyle1 = new PHPExcel_Style();
		$sharedStyle2 = new PHPExcel_Style();
		$sharedStyle3 = new PHPExcel_Style();
		$sharedStyle4 = new PHPExcel_Style();
		$end = new PHPExcel_Style();
		$sharedStyle1->applyFromArray(
			array('fill' 	=> array(
										'type'		=> PHPExcel_Style_Fill::FILL_SOLID,
										'color'		=> array('argb' => 'FFCCFFCC')
									),
				  'borders' => array(
										'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
										'right'		=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
									)
				 ));

		$sharedStyle2->applyFromArray(
			array('fill' 	=> array(
										'type'		=> PHPExcel_Style_Fill::FILL_SOLID,
										'color'		=> array('argb' => 'FFFFFF00')
									),
					'borders' => array(
						'bottom'     => array(
							'style' => PHPExcel_Style_Border::BORDER_DASHDOT
						) ,
						'top'     => array(
							'style' => PHPExcel_Style_Border::BORDER_DOTTED
						) ,
						'right'		=> array('style' => PHPExcel_Style_Border::BORDER_THIN)
					),
					'alignment' => array(
						'wrap' => true
						)		  
				 ));
				 
		$sharedStyle3->applyFromArray(
			array(
                                         'borders' => array(
						'bottom'     => array(
							'style' => PHPExcel_Style_Border::BORDER_THIN ,
                                                        'color' => array('argb' => '00000000'),
						) ,
                                             'right'     => array(
							'style' => PHPExcel_Style_Border::BORDER_THIN ,
                                                        'color' => array('argb' => '00000000'),
						) ,
                                                'top'     => array(
							'style' => PHPExcel_Style_Border::BORDER_THIN ,
                                                        'color' => array('argb' => '00000000'),
						) ,
					),                               
					
					'alignment' => array(
						'wrap' => true
						)		  
				 ));		 
				 
		$sharedStyle4->applyFromArray(
			array('fill' 	=> array(
										'type'		=> PHPExcel_Style_Fill::FILL_SOLID,										
									),
					'borders' => array(
						'bottom'     => array(
							'style' => PHPExcel_Style_Border::BORDER_DASHDOT
						) ,
						'top'     => array(
							'style' => PHPExcel_Style_Border::BORDER_DOTTED
						) ,
						'right'		=> array('style' => PHPExcel_Style_Border::BORDER_THIN)
					),
					'alignment' => array(
						'wrap' => true
						)		  
				 ));
		$styleThickBrownBorderOutline = array(
			'borders' => array(
				'outline' => array(
					'style' => PHPExcel_Style_Border::BORDER_THICK,
					'color' => array('argb' => 'FF993300'),
				),
			),
		);

		$styleThinBlackBorderOutline = array(
			'borders' => array(
				'top' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
					'color' => array('argb' => 'FF000000'),
				),
				'right' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
					'color' => array('argb' => 'FF000000'),
				),
				'bottom' => array(
					'style' => PHPExcel_Style_Border::BORDER_DOTTED,
					'color' => array('argb' => 'FF000000'),
				),
			),
		);
		
                $end->applyFromArray(
			array(
                                        'borders' => array(
                                               
                                                'bottom' => array(
                                                        'style' => PHPExcel_Style_Border::BORDER_THIN,
                                                        'color' => array('argb' => 'FF000000'),
                                                ),
                                            'right' => array(
                                                        'style' => PHPExcel_Style_Border::BORDER_THIN,
                                                        'color' => array('argb' => 'FF000000'),
                                                ),
                                                
                                        ),                                    
                                        
					'alignment' => array(
						'wrap' => true
						)		  
				 ));
                
		$objPHPExcel->getActiveSheet()->setCellValue('A1', $title);		
		//
		$objPHPExcel->getActiveSheet()->setCellValue('A2', "Phòng ban yêu cầu");
		$objPHPExcel->getActiveSheet()->setCellValue('B2', "Giờ tiếp nhận");
		$objPHPExcel->getActiveSheet()->setCellValue('C2', "Giờ hoàn thành");
		$objPHPExcel->getActiveSheet()->setCellValue('D2', "Nhóm công việc");
		$objPHPExcel->getActiveSheet()->setCellValue('E2', "Nội dung công việc");
		$objPHPExcel->getActiveSheet()->setCellValue('F2', "Cách xử lý");	
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->setCellValue('G2', "Nhân viên");	
		}		
		//
		$i = 3;
		foreach($data as $job)
		{			
			$objPHPExcel->getActiveSheet()->setCellValue("A$i", $job['department']);
			$job['received_time'] = Common::convertStringToDateTime($job['received_time']);
			$job['finished_time'] = Common::convertStringToDateTime($job['finished_time']);			
			$objPHPExcel->getActiveSheet()->setCellValue("B$i", $job['received_time']);
			$objPHPExcel->getActiveSheet()->setCellValue("C$i", $job['finished_time']);
			$objPHPExcel->getActiveSheet()->setCellValue("D$i", $job['job_group_name']);
			$objPHPExcel->getActiveSheet()->setCellValue("E$i", $job['detail']);
			$objPHPExcel->getActiveSheet()->setCellValue("F$i", $job['solution']);	
			if($type != 'user')
			{
				$objPHPExcel->getActiveSheet()->setCellValue("G$i", $job['full_name']);	
			}
			$i++;
		}
                
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(35);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
		}
		$objPHPExcel->getActiveSheet()->getRowDimension(2)->setRowHeight(40);
		
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Arial');
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('Arial');		
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->setName('Arial');		
		$objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setName('Arial');		
		$objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->setName('Arial');		
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->setName('Arial');				
		$objPHPExcel->getActiveSheet()->getStyle('F2')->getFont()->setName('Arial');
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->getStyle('G2')->getFont()->setName('Arial');
		}
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->setAutoFilter("A2:G$i");	
		}
		else
		{
			$objPHPExcel->getActiveSheet()->setAutoFilter("A2:F$i");	// Always include the complete filter range!
		}
                $i--;
                if($type != 'user')
		{
                   $objPHPExcel->getActiveSheet()->setSharedStyle($end , "A$i:G$i");
		}
                else
                {
                    $objPHPExcel->getActiveSheet()->setSharedStyle($end , "A$i:F$i");
                }
                $i--;
              
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "A2:A$i");
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "B2:B$i");
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "C2:C$i");
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "D2:D$i");
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "E2:E$i");
		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "F2:F$i");
               
                 
		if($type != 'user')
		{
                   $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle4, "G2:G$i");
		}
                if($type != 'user')
                {
                    $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle3, "A2:G2");
                }
                else
                {
                     $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle3, "A2:F2");
                }
                
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->getStyle("A2:G$i")->applyFromArray($styleThinBlackBorderOutline);
		}
		else
		{
			$objPHPExcel->getActiveSheet()->getStyle("A2:F$i")->applyFromArray($styleThinBlackBorderOutline);
		}
		
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
		}
		else
		{
			$objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
		}
		
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('F2')->getFont()->setBold(true);
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->getStyle('G2')->getFont()->setBold(true);
		}
		
                
                $objPHPExcel->getActiveSheet()->getStyle("A3:A$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                
		$objPHPExcel->getActiveSheet()->getStyle("B3:B$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                $objPHPExcel->getActiveSheet()->getStyle("C3:C$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                
                 $objPHPExcel->getActiveSheet()->getStyle("D3:D$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                 
                   $objPHPExcel->getActiveSheet()->getStyle("D3:D$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                   
                     $objPHPExcel->getActiveSheet()->getStyle("E3:E$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                     
                       $objPHPExcel->getActiveSheet()->getStyle("F3:F$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                     if($type != 'user')
                     {
                            $objPHPExcel->getActiveSheet()->getStyle("G3:G$i")->getAlignment()->applyFromArray(
                             array(
                                 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                                 'vertical'   => PHPExcel_Style_Alignment::VERTICAL_TOP,
                                 'rotation'   => 0,
                                 'wrap'       => true
                             )
                     );
                     }
                       
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('D2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('F2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('D2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		if($type != 'user')
		{
			$objPHPExcel->getActiveSheet()->getStyle('G2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$objPHPExcel->getActiveSheet()->getStyle('G2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		}
		$objPHPExcel->getActiveSheet()->setTitle('helpdesk_onsite');
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);

		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="helpdesk_onsite.xls"');
		header('Cache-Control: max-age=0');

      	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        ob_end_clean();
        
        $objWriter->save('php://output');
        $objPHPExcel->disconnectWorksheets();
        unset($objPHPExcel);
		Yii::app()->end();

	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Job::model()->findByPk($id);
		//$model->received_time = date("h:i d-m-Y", strtotime($model->received_time));
		//$model->finished_time = date("h:i d-m-Y", strtotime($model->finished_time));
                $model->received_time = Common::convertStringToDateTime($model->received_time);
                $model->finished_time = Common::convertStringToDateTime($model->finished_time);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='job-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
